package com.sunxiansheng.exception.strategys;
// 注意这里引入的包
import com.sunxiansheng.exception.response.RespBeanEnum;
import com.sunxiansheng.exception.response.Result;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
/**
 * Description: 参数绑定异常的策略
 * @Author sun
 * @Create 2024/7/11 18:14
 * @Version 1.0
 */
@Component
public class BindExceptionStrategy implements ExceptionStrategy {

    @Override
    public boolean supports(Exception e) {
        return e instanceof BindException;
    }

    @Override
    public Result handleException(Exception e) {
        BindException ex = (BindException) e;
        String message = "参数校验异常：" + ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        return Result.fail(RespBeanEnum.BING_ERROR.getCode(), message);
    }

}